<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="head-portrait">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 登录区域 -->
      <div class="input-box">
        <el-form ref="loginFormRef" :model="form" :rules="rules">
          <!-- 用户名框 -->
          <el-form-item prop="username">
            <el-input v-model="form.username" placeholder="用户名" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <!-- 密码框 -->
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              placeholder="密码"
              prefix-icon="iconfont icon-3702mima"
            ></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <div class="btn">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetForm('loginFormRef')">重置</el-button>
          </div>
        </el-form>
        <!-- 按钮区域 -->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 7, message: '长度在 1 到 7 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        // console.log(valid);
        if (!valid) return
        //发起请求需要axios,需要再全局main.js文件中进行配置
        //data是promise返回的我们需要的数据 {data:data}是解构赋值,得到我们想要的数据,第二个data是重命名的
        const { data: data } = await this.$http.post('login', this.form) //第一个参数是路径,第二个参数是用户输入的val
        //console.log(data) //请求结果是一个promise,可以用async awite简化操作
        if (data.meta.status != 200) return this.$message.error('登录失败!')
        this.$message.success('登录成功')
        //1.将登陆成功之后的token保存到客户端的sessionStorage中
        //1.1 项目中除了登录之外的其他API节后,必须在登录之后才能访问
        //1.2 token 只应该在当前网站打开期间生效,所以将token 保存在 sessionStorge中
        window.sessionStorage.setItem('token', data.data.token)
        //2.通过编程式导航跳转奥后台主页,路由地址是/home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less"  scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 540px;
  height: 350px;
  background-color: #fff;
  border-radius: 6px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.head-portrait {
  width: 133px;
  height: 133px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -40%);
  background-color: #fff;
  box-shadow: 0 0 10px #eee;
}
img {
  width: 133px;
  height: 133px;
  border-radius: 50%;
  background-color: #eee;
}
.input-box {
  position: absolute;
  bottom: 40px;
  width: 100%;
  box-sizing: border-box;
  padding-left: 20px;
  padding-right: 20px;
}
.btn {
  display: flex;
  justify-content: flex-end;
}
</style>